//
//  VideoModel.m
//  VideoExam
//
//  Created by apple on 2020/4/11.
//

#import "VideoModel.h"

@implementation VideoModel
- (instancetype)init
{
    self = [super init];
    if (self) {
        _Upload=NO;
        _opportunity_number=3;
        _showType=Cell_show_NO_Recording_type;
        _cellHeight=90;
    }
    return self;
}
#pragma mark - < NSCoding >
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [self yy_modelEncodeWithCoder:aCoder];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    return [self yy_modelInitWithCoder:aDecoder];
}
///设置视频录制完成时间
- (void)set_complete_date_str {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy.MM.dd.HH:mm"];
    NSString *dateStr = [formatter stringFromDate:[NSDate date]];
    self.complete_date_str =dateStr;
}
#pragma mark 完成录制设置 model
/// 完成录制设置 model
/// @param videoName 视频名称
/// @param video_length 视频时长
- (void)video_record_complete_set_videoName:(NSString *)videoName video_length:(NSString *)video_length {
  if (self.opportunity_number!=3 && [self.videoName isHelpful]) {
        [self delete_video_File];//删除沙盒视频文件
   }
   self.videoName=videoName;
   self.showType=Cell_show_Already_Recording_type;
   self.opportunity_number--;//减去录制机会次数
   self.video_length=video_length;//视频时长
    self.current_account = [UserModel get_userModel].phone;//当前账号
   [self set_complete_date_str];//设置视频录制完成时间
}

#pragma mark 根据订单号获取 本地未上传视频model数组
/// 根据订单号ID 本地未上传视频model数组
+ (NSArray *)get_not_upload_local_dataArray_orderId:(NSString *)orderId {
    if ([orderId isHelpful]==NO) {
        return [NSArray new];
    }
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *data =   [userDefaults objectForKey:orderId];
    NSMutableArray *modelArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return modelArray;
}
#pragma mark 上传错误保存本地
/// 上传错误保存本地
/// @param dataArray dataArray
/// @param OrderId 订单ID
+ (void)save_local_ModelArray:(NSArray *)dataArray OrderId:(NSString *)OrderId {
    if ([OrderId isHelpful]==NO) {
        return;
    }
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dataArray];
        [userDefaults setObject:data forKey:OrderId];
        [userDefaults synchronize];//  同步数据
         NSLog(@"保存未上传信息 订单号 %@ 当前线程%@",OrderId,[NSThread currentThread]);
    });
}
#pragma mark 上传成功后删除本地 原视频 和 压缩后的视频 都删除

/// 上传成功后删除本地 原视频 和 压缩后的视频 都删除
/// @param orderId 订单id
/// @param dataArray 视频上传界面的数组
+(void)remove_local_ModelArray_OrderId:(NSString *)orderId  dataArray:(NSMutableArray *)dataArray {
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            for (VideoModel *model in dataArray) {
                [model delete_video_File];//删除沙盒视频文件
                [model delete_compression_video_File]; //删除本压缩后的地视频文件
            }
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults removeObjectForKey:orderId];//产出NSUserDefaults 里的model
     });
    
}

#pragma mark  删除本地视频文件 无回调
///删除本地视频文件无回调
- (void)delete_video_File {
  BOOL isSuccess = NO;
  NSError *error;
  NSFileManager *fileManager = [NSFileManager defaultManager];
  isSuccess = [fileManager removeItemAtPath:[self get_original_path] error:&error];
  if (error) {
      NSLog(@"removeFile 失败：%@",[error localizedDescription]);
  }else{
      NSLog(@"removeFile 成功");
  }
}

#pragma mark  删除本地视频文件 带回调成功失败都回调
/// 删除本地视频文件 带回调成功失败都回调
/// @param block block
- (void)delete_video_File_block:(void(^)(void))block {
    BOOL isSuccess = NO;
     NSError *error;
     NSFileManager *fileManager = [NSFileManager defaultManager];
     isSuccess = [fileManager removeItemAtPath:[self get_original_path] error:&error];
     if (error) {
         if (block) {
             block();
         }
         NSLog(@"removeFile 失败：%@",[error localizedDescription]);
     }else{
         if (block) {
            block();
        }
         NSLog(@"removeFile 成功");
     }
}
#pragma mark  删除本压缩后的地视频文件
///删除本压缩后的地视频文件
- (void)delete_compression_video_File {
  BOOL isSuccess = NO;
  NSError *error;
  NSFileManager *fileManager = [NSFileManager defaultManager];
  isSuccess = [fileManager removeItemAtPath:[self get_compression_path] error:&error];
  if (error) {
      NSLog(@"removeFile压缩 失败：%@",[error localizedDescription]);
  }else{
      NSLog(@"removeFile压缩 成功");
  }
}



//+(NSDictionary*)getFileInfo:(NSString*)path{
//    NSError *error;
//    NSDictionary *reslut =  [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
//    if (error) {
//        NSLog(@"getFileInfo Failed:%@",[error localizedDescription]);
//    }
//    return reslut;
//}

#pragma mark 获取本地视频封面 此方法 cell 里使用的
//获取本地视频封面 此方法 cell 里使用的
- (UIImage *)get_cover_Image {
  NSURL *url = [NSURL fileURLWithPath:[self get_original_path]];
  return  [AppTools getVideoPreViewImage:url];
}

#pragma mark 获取视频沙盒路径
//获取视频沙盒路径
- (NSString *)get_original_path {
    //沙盒路径
  NSString * documents_path= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];
  NSString * path =[NSString stringWithFormat:@"%@/%@",documents_path,self.videoName];
  return path;
}

#pragma mark 获取视频压缩后的路径
///获取视频压缩后的路径
- (NSString *)get_compression_path {
    NSString * documents_path= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];
    NSString * path =[NSString stringWithFormat:@"%@/%@",documents_path,self.compression_video_name];
    return path;
}

#pragma mark  获取 Document 路径
/// 获取 Document 路径
- (NSString *)get_document_path {
    NSString *document_path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];
    NSString * path=[NSString stringWithFormat:@"%@/",document_path];
    return path;
}
#pragma mark 获取时间戳名
///获取时间戳名
- (NSString *)get_date_name {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *date_name = [formatter stringFromDate:[NSDate date]];
    NSString * name = [NSString stringWithFormat:@"%@.mp4",date_name];
    return name;
}
#pragma mark 长传视频时设置 oss_url_pathName
/// 长传视频时设置 oss_url_pathName
- (NSString *)set_oss_url_pathName {
    NSString * path_name = [NSString stringWithFormat:@"%@%@",self.oss_url_path,self.compression_video_name];
    return path_name;
}





@end
